package com.example.administrator.xxnapp.friend;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.administrator.xxnapp.BaseActivity;
import com.example.administrator.xxnapp.R;
import com.example.administrator.xxnapp.bean.UserBean;
import com.example.administrator.xxnapp.util.Config;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mdruby.baselibrary.utils.ObjectStoreUtil;

public class FriendActivity extends BaseActivity implements IViewFriend{
    @BindView(R.id.act_friend_RV)
    RecyclerView mRV;
    private List<UserBean> mDatas;
    private RVFriendAdapter mAdapter;
    private FriendPresenter mPresenter;
    private UserBean user;

    @Override
    public int getLayoutId() {
        return R.layout.activity_friend;
    }

    @Override
    public void initBeforeDatas() {
        mDatas = new ArrayList<>();
        mPresenter = new FriendPresenter(this,new UserBean());
        user = (UserBean) ObjectStoreUtil.getObject(mContext, Config.Key.USER_OBJECT_KEY);
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
    }

    @Override
    public void setListeners() {
        mRV.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new RVFriendAdapter(mContext,mDatas);
        mRV.setAdapter(mAdapter);

        mAdapter.setOnFriendItemClickListener(new RVFriendAdapter.OnFriendItemClickListener() {
            @Override
            void onItemClick(int position) {
                UserBean userBean = mDatas.get(position);
                Intent intent = getIntent();
                intent.putExtra(Config.ShareKey.FRIEND,userBean);
                setResult(RESULT_OK,intent);
                FriendActivity.this.finish();
            }
        });

        mPresenter.getFriends(user.getUserId());
    }

    @Override
    public void getFriendsSuccess(List<UserBean> beans) {
        mDatas.clear();
        mDatas.addAll(beans);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void getFriendsError(int resultCode, String message) {

    }
}
